import axios from 'axios'
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      inputText: '',
      dialogFormVisible: false,
      labelPosition: 'right',
      addUserForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      }
    }
  },
  created () {
    this.loadUsersByPage(1);
  },
  methods: {

    handelCurrentChange (page) {
      this.loadUsersByPage(page)
    },
    /**
     * 分页请求列表
     *
     */
    loadUsersByPage (page) {
      axios.get('http://localhost:8888/api/private/v1/users',{
        headers: {
          Authorization: window.localStorage.getItem('token')
        },
        params: {
           pagenum:page, // 告诉服务器要第一页的数据
           pagesize: 5,
           query:this.inputText.trim()
        },
      }).then(res => {
        // console.log(window.localStorage)
        const {data,meta} = res.data
        if (meta.status === 200) {
          this.tableData = data.users
          this.total = data.total
        }
      })
    },
    /**
     *处理搜索
     * @return {[type]} [description]
     */
    handelSearch () {

      this.loadUsersByPage(1)

    },
    /**
     * 添加用户
     */
    handelAddUser () {
      console.log(this.addUserForm)
      // 第一个参数是实例
      axios({
        method: 'post',
        url: 'http://localhost:8888/api/private/v1/users',
        data: this.addUserForm,
        headers: {
          Authorization: window.localStorage.getItem('token')
        }
      }).then(res => {
        if(res.data.meta.status === 201){
          this.$message({
            type: 'success',
            message: '添加用户成功'
          })
          // 关闭弹窗
          this.dialogFormVisible = false
          // 清空表单
          this.$refs['addUserForm'].resetFields()
        }
      })
    }
  }
}
